﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace ZK.Common
{
    public class Utility
    {
        /// <summary>
        /// 数字和字母随机数
        /// </summary>
        /// <param name="n">生成长度</param>
        /// <returns></returns>
        public static string RandCode(int n)
        {
            char[] arrChar = new char[]{
           'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
           '0','1','2','3','4','5','6','7','8','9',
           'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
          };
            StringBuilder num = new StringBuilder();
            Random rnd = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < n; i++)
            {
                num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
            }
            return num.ToString();
        }
        public static int StringToIntParse(string strChars)
        {
            int iRe = 0;
            if (string.IsNullOrEmpty(strChars))
            {
                return 0;
            }
            int.TryParse(strChars, out iRe);
            return iRe;
        }
        public static bool IsConn()
        {
            try
            {
                string strUrl = Settings.GetAppConfig("WebAppUrl");
                WebRequest myRequest = WebRequest.Create(strUrl);
                WebResponse myResponse = myRequest.GetResponse();
                myResponse.Close();
                return true;
            }
            catch (Exception ex)
            {
                Log4.LogManage.WriteErr("IsConn " + ex.Message);
                // Logger.e("外网没有连接:" + ex.Message);
                return false;
            }

        }
        /// <summary>
        /// 获取displayName
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="proName"></param>
        /// <returns></returns>
        public static string GetDisplayName<T>(string proName)
        {
            var t = typeof(T);
            var pName = t.GetProperty(proName);
            var displayName = pName.GetCustomAttribute<DisplayNameAttribute>();
            if (displayName != null)
            {
                return displayName.DisplayName;
            }
            return "";
        }
        public static bool IsNumber(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }
        public static bool IsInt(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*$");
        }
        public static bool IsUnsign(string value)
        {
            return Regex.IsMatch(value, @"^\d*[.]?\d*$");
        }

        public static bool isTel(string strInput)
        {
            return Regex.IsMatch(strInput, @"\d{3}-\d{8}|\d{4}-\d{7}");
        }

        /// <summary>
        /// 获取没有设置displayname的属性的下坐标
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<int> GetDisplayIndex<T>()
        {
            List<int> lst = new List<int>();
            var t = typeof(T);
            var propertyInfos = t.GetProperties();
            for (int i = 0; i < propertyInfos.Length; i++)
            {
                var displayNameAttribute = propertyInfos[i].GetCustomAttribute<DisplayNameAttribute>();
                if (displayNameAttribute == null)
                {
                    lst.Add(i);
                }
            }
            return lst;

        }
    }
}